package handler

import (
	"github.com/gin-gonic/gin"
	"go_shengshu_admin/enum"
	"go_shengshu_admin/model"
	"go_shengshu_admin/query"
	"go_shengshu_admin/resp"
	"go_shengshu_admin/service"
	"net/http"
)

type BannerHandler struct {
	Srv service.BannerSrv
}

func (h *BannerHandler) GetEntity(banner model.Banner) resp.Banner {
	return resp.Banner{
		BannerId:    banner.BannerId,
		Url:         banner.Url,
		RedirectUrl: banner.RedirectUrl,
		Order:       banner.Order,
	}
}

func (h *BannerHandler) BannerInfoHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		TotalPage: 0,
		Total:     0,
	}
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	banner := model.Banner{
		BannerId: id,
	}
	result, err := h.Srv.GetBanner(banner)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	r := h.GetEntity(*result)
	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      r,
		TotalPage: 0,
		Total:
		0,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *BannerHandler) BannerListHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		TotalPage: 0,
		Total:     0,
	}
	var q query.ListQuery
	if err := c.ShouldBindQuery(&q); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}

	list, err := h.Srv.List(&q)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	total, err := h.Srv.GetTotal()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}

	pageSize := q.PageSize
	if pageSize == 0 {
		pageSize = 5
	}
	pageTotal := 0
	if total != 0 {
		ret := int(total % pageSize)
		ret2 := int(total / pageSize)
		if ret == 0 {
			pageTotal = ret2
		} else {
			pageTotal = ret2 + 1
		}
	}

	var newList []*resp.Banner
	for _, item := range list {
		repBanner := h.GetEntity(*item)
		newList = append(newList, &repBanner)
	}
	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      newList,
		TotalPage: pageTotal,
		Total:     total,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *BannerHandler) AddBannerHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		TotalPage: 0,
		Total:     0,
	}
	var banner model.Banner
	if err := c.ShouldBindJSON(&banner); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}

	restult, err := h.Srv.Add(banner)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	r := h.GetEntity(*restult)
	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      r,
		TotalPage: 0,
		Total:     0,
	}

	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *BannerHandler) EditBannerHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		TotalPage: 0,
		Total:     0,
	}
	var banner model.Banner
	if err := c.ShouldBindJSON(&banner); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	b, err := h.Srv.Edit(banner)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	if !b {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *BannerHandler) DeleteBannerHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		TotalPage: 0,
		Total:     0,
	}
	id := c.Param("id")
	b, err := h.Srv.Delete(id)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	if !b {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}
